//
//  HYOnlineGameViewController.m
//  HaiYue
//
//  Created by skyline on 2017/12/18.
//  Copyright © 2017年 skyline. All rights reserved.
//

#import "HYGameTableView.h"
#import "HYOnlineTopCell.h"
#import "HYOnlineMidCell.h"
@interface HYGameTableView ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, assign) CGFloat onlineTopCell_H;
@property (nonatomic, assign) CGFloat onlineMidCell_H;
@end

@implementation HYGameTableView
- (instancetype)initWithFrame:(CGRect)frame style:(UITableViewStyle)style
{
    self = [super initWithFrame:frame style:style];
    if (self)
    {
        self.type = HYGameTableViewTypeDefault;
        self.separatorStyle = UITableViewCellSeparatorStyleNone;
        self.delegate = self;
        self.dataSource = self;
        
    }
    return self;
}

#pragma mark - UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    if (!self.model)
    {
        return 0;
    }
    return (self.type == HYGameTableViewTypeCycleHeader)?3:1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_type == HYGameTableViewTypeCycleHeader && (section == 0 || section == 1)) return 1;
    else if([self.model.tabgames count] > 0)
    {
        return self.model.tabgames.count;
    }
    else if ([self.model.bookgames count] > 0)
    {
        return self.model.bookgames.count;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = (indexPath.section == 0)?@"HYOnlineTopCell":((indexPath.section == 1)?@"HYOnlineMidCell":@"HYOnlineCell");
    if (indexPath.section == 0 && (_type == HYGameTableViewTypeCycleHeader))
    {
        HYOnlineTopCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell)
        {
            cell = [[HYOnlineTopCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        self.onlineTopCell_H = cell.H;
        cell.topgames = self.model.topgames;
        cell.bookgames = self.model.bookgames;
        return cell;
    }
    else if (indexPath.section == 1 && (_type == HYGameTableViewTypeCycleHeader))
    {
        HYOnlineMidCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell)
        {
            cell = [[HYOnlineMidCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        self.onlineMidCell_H = cell.H;
        return cell;
    }
    else
    {
        HYOnlineCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
        if (!cell)
        {
            cell = [[HYOnlineCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:identifier];
        }
        cell.model = self.model.tabgames[indexPath.row];
        return cell;
    }
    return nil;
}

#pragma mark - UITableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) return self.onlineTopCell_H;
    else if (indexPath.section == 1) return self.onlineMidCell_H;
    else return HYOnlineCellHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (self.hydelegate && [self.hydelegate respondsToSelector:@selector(scrollViewDidScroll:)])
    {
        [self.hydelegate scrollViewDidScroll:scrollView];
    }
}

#pragma mark - HYOnlineTopCellDelegate
- (void)methodForHYOnlineTopCellSelectedGame:(NSInteger)index
{
    
}
- (void)methodForHYOnlineTopCellSelectedCycleIMG:(NSInteger)index
{
    
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
